﻿Imports System.Drawing

Public Class Styles

    Friend FDatabase As Database = Nothing
    Friend FStyleData As Data.StylesDataTable = Nothing
    Friend FProjectletData As Data.ProjectletsDataTable = Nothing

    Friend Sub New(ByVal database As Database)
        FDatabase = database
        FStyleData = database.Data.Styles
        FProjectletData = database.Data.Projectlets
    End Sub

    Public Function GetAllStyles() As List(Of Style)
        Dim Result As New List(Of Style)
        For Each Row As Data.StylesRow In FStyleData
            Result.Add(CreateStyle(Row, FProjectletData.Select("StyleID='" + Row.StyleID.ToString + "'")))
        Next
        Return Result
    End Function

    Public Function GetAllStyleNames() As List(Of String)
        Dim Result As New List(Of String)
        For Each Row As Data.StylesRow In FStyleData
            Result.Add(Row.Name)
        Next
        Return Result
    End Function

    Public Function GetStyle(ByVal styleName As String) As Style
        styleName = styleName.ToUpper.Trim
        For Each Row As Data.StylesRow In FStyleData
            If Row.Name.ToUpper.Trim = styleName Then
                Return CreateStyle(Row, FProjectletData.Select("StyleID='" + Row.StyleID.ToString + "'"))
            End If
        Next
        Throw New ApplicationException("Cannot find the style with the name '" + styleName + "'.")
    End Function

    Public Function GetDefaultStyle() As Style
        If GetAllStyleNames.Contains("Default") Then
            Return GetStyle("Default")
        Else
            Return New Style("Default")
        End If
    End Function

    Public Function GetStyle(ByVal styleID As Guid) As Style
        Return CreateStyle(FStyleData.FindByStyleID(styleID), _
                           FProjectletData.Select("StyleID='" + styleID.ToString + "'"))
    End Function

    Public Function TryGetStyle(ByVal styleID As Guid, _
                                ByVal defaultStyle As Style) As Style

        Dim StyleFound As Boolean = FStyleData.FindByStyleID(styleID) IsNot Nothing
        If StyleFound Then
            Return GetStyle(styleID)
        Else
            Return defaultStyle
        End If

    End Function

    Public Sub AddStyle(ByVal style As Style)

        ' Create the row for the tyle in the XML database
        Dim StyleID As Guid = Guid.NewGuid
        style.StyleID = StyleID
        FStyleData.AddStylesRow(StyleID, style.Name, style.DefaultBackgroundImage, _
                                style.DefaultBackgroundColour.ToArgb, _
                                style.GeneralFadeDuration, style.VerseFade, _
                                style.VerseFadeDuration, style.GeneralFade, _
                                style.BackgroundType)

        ' Create rows for projectlets
        For Each Item As ProjectletStyle In style.Projectlets
            FProjectletData.AddProjectletsRow(FStyleData.FindByStyleID(StyleID), Item.Category, _
                                              Item.FontName, Item.FontSize, Item.FontColour.ToArgb, _
                                              Item.FontStyle, Item.FontEffect, Item.FontEffectColour.ToArgb, _
                                              Item.Area.Left, Item.Area.Top, Item.Area.Width, Item.Area.Height, _
                                              Item.Alignment, Item.DisplayOption, Item.Content)
        Next

        ' Save the database
        FDatabase.Save()

    End Sub

    Public Sub DeleteStyle(ByVal style As Style)
        DeleteStyle(style.StyleID)
    End Sub

    Public Sub DeleteStyle(ByVal styleID As Guid)

        ' First remove all projectlet data
        Dim RowsToDelete As New List(Of Data.ProjectletsRow)
        For Each Row As Data.ProjectletsRow In FProjectletData
            If Row.StyleID = styleID Then RowsToDelete.Add(Row)
        Next
        For Each Row As Data.ProjectletsRow In RowsToDelete
            Row.Delete()
        Next

        ' Remove the style row
        FStyleData.RemoveStylesRow(FStyleData.FindByStyleID(styleID))

        ' Save changes
        FDatabase.Save()

    End Sub

    Public Sub EditStyle(ByVal styleID As Guid, _
                         ByVal style As Style)

        ' Internal check
        If style.StyleID <> styleID Then Throw New ApplicationException("A style is being edited but an invalid style ID was provided.")

        ' Change the main style row
        Dim StyleRow As Data.StylesRow = FStyleData.FindByStyleID(styleID)
        StyleRow.StyleID = style.StyleID
        StyleRow.BackgroundType = style.BackgroundType
        StyleRow.DefaultBackgroundImage = style.DefaultBackgroundImage
        StyleRow.DefaultBackgroundColour = style.DefaultBackgroundColour.ToArgb
        StyleRow.GeneralFade = style.GeneralFade
        StyleRow.GeneralFadeDuration = style.GeneralFadeDuration
        StyleRow.VerseFade = style.VerseFade
        StyleRow.VerseFadeDuration = style.VerseFadeDuration
        StyleRow.Name = style.Name

        ' Change the projectlet styles
        For Each Projectlet As ProjectletStyle In style.Projectlets

            Dim SubRow As Data.ProjectletsRow = FProjectletData.FindByStyleIDComponent(styleID, Projectlet.Category)
            SubRow.FontName = Projectlet.FontName
            SubRow.FontSize = Projectlet.FontSize
            SubRow.FontColour = Projectlet.FontColour.ToArgb
            SubRow.FontStyle = Projectlet.FontStyle
            SubRow.FontEffect = Projectlet.FontEffect
            SubRow.FontEffectColour = Projectlet.FontEffectColour.ToArgb
            SubRow.Alignment = Projectlet.Alignment
            SubRow.AreaLeft = Projectlet.Area.Left
            SubRow.AreaTop = Projectlet.Area.Top
            SubRow.AreaWidth = Projectlet.Area.Width
            SubRow.AreaHeight = Projectlet.Area.Height
            SubRow.WhenToDisplay = Projectlet.DisplayOption
            SubRow.Content = Projectlet.Content

        Next

        ' Save changes
        FDatabase.Save()

    End Sub

    Private Function CreateStyle(ByVal styleRow As Data.StylesRow, _
                                 ByVal projectletRows As Data.ProjectletsRow()) As Style

        Dim Result As New Style(styleRow.Name)
        Result.StyleID = styleRow.StyleID
        Result.DefaultBackgroundImage = styleRow.DefaultBackgroundImage
        Result.DefaultBackgroundColour = Color.FromArgb(styleRow.DefaultBackgroundColour)
        Result.BackgroundType = styleRow.BackgroundType
        Result.GeneralFade = styleRow.GeneralFade
        Result.GeneralFadeDuration = styleRow.GeneralFadeDuration
        Result.VerseFade = styleRow.VerseFade
        Result.VerseFadeDuration = styleRow.VerseFadeDuration

        For Each ProjectletRow As Data.ProjectletsRow In projectletRows

            Dim Projectlet As ProjectletStyle = Result.ProjectletsByName(ProjectletRow.Component)
            Projectlet.FontEffect = ProjectletRow.FontEffect
            Projectlet.FontEffectColour = Color.FromArgb(ProjectletRow.FontEffectColour)
            Projectlet.FontName = ProjectletRow.FontName
            Projectlet.FontStyle = ProjectletRow.FontStyle
            Projectlet.FontSize = ProjectletRow.FontSize
            Projectlet.FontColour = Color.FromArgb(ProjectletRow.FontColour)
            Projectlet.Alignment = [Enum].Parse(GetType(Style.ETextAlignment), ProjectletRow.Alignment)
            Projectlet.Area = New RectangleF(ProjectletRow.AreaLeft, ProjectletRow.AreaTop, ProjectletRow.AreaWidth, ProjectletRow.AreaHeight)
            Projectlet.DisplayOption = [Enum].Parse(GetType(Style.EDisplayOption), ProjectletRow.WhenToDisplay)
            Projectlet.Content = ProjectletRow.Content

        Next

        Return Result

    End Function

End Class
